//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.service;

import cn.hutool.core.util.StrUtil;
import com.example.entity.Account;
import com.example.entity.Student;
import com.example.exception.CustomException;
import com.example.mapper.StudentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class StudentService {
    @Resource
    private StudentMapper studentMapper;

    public StudentService() {
    }

    public void add(Student student) {
        String username = student.getUsername();
        Student dbstudent = this.studentMapper.selectByUsername(username);
        if (dbstudent != null) {
            throw new CustomException("500", "用户名已存在，请更换其他用户名");
        } else {
            if (StrUtil.isBlank(student.getPassword())) {
                student.setPassword("123"); // 默认密码 123
            }

            if (StrUtil.isBlank(student.getName())) {
                student.setName(student.getUsername());
            }

            student.setRole("STU");  //必须设置role
            this.studentMapper.insert(student);

        }
    }

    public void updateById(Student student) {
        this.studentMapper.updateById(student);
    }

    public void deleteById(Integer id) {
        this.studentMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
        Iterator var2 = ids.iterator();

        while (var2.hasNext()) {
            Integer id = (Integer) var2.next();
            this.deleteById(id);
        }

    }

    public Student selectById(Integer id) {
        return this.studentMapper.selectById(id);
    }

    public List<Student> selectAll(Student student) {
        return this.studentMapper.selectAll(student);
    }

    public List<Student> selectList(Student student) {
        System.out.println(student);
        return null;
    }

    public PageInfo<Student> selectPage(Student student, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> list = this.studentMapper.selectAll(student);
        return PageInfo.of(list);
    }

    public Student login(Account account) {
        String username = account.getUsername();
        Student dbstudent = this.studentMapper.selectByUsername(username);
        if (dbstudent == null) {
            throw new CustomException("500", "用户不是学生");
        } else {
            String password = account.getPassword();
            if (!dbstudent.getPassword().equals(password)) {
                throw new CustomException("500", "密码错误，请重新输入");
            } else {
                return dbstudent;
            }
        }
    }

    public void register(Student student) {
        this.add(student);
    }

    public void updatePassword(Account account) {
        Integer id = account.getId();
        Student dbstudent = this.selectById(id);
        if (!dbstudent.getPassword().equals(account.getPassword())) {  //最不可能错的放在对比的左侧。如果数据库中的密码和提交表单的密码不一致
            throw new CustomException("500", "原密码错误");
        } else {
            dbstudent.setPassword(account.getNewPassword());
            this.updateById(dbstudent);
        }
    }

    public Student selectByUsername(String username) {
        return this.studentMapper.selectByUsername(username);
    }
}

